import 'package:flutter_bloc/flutter_bloc.dart';

class MultiStateBloc extends Bloc<MultiStateEvent, MultiState> {
  int _count = 0;

  MultiStateBloc(MultiState initialState) : super(initialState);

  // 这里如果只想改变state中的成员变量，可以这样写：
  // CounterState get _currentCounterState => CounterState(_title, _count);
  // _title = event.title;
  // return _currentCounterState;
  // 具体见：CounterBloc
  @override
  Stream<MultiState> mapEventToState(MultiStateEvent event) async* {
    if (event is DataChangeEvent) {
      yield PageViewState(true, true);
      await Future.delayed(Duration(milliseconds: 1000));
      yield DataState((event.data ?? "") + _count.toString());
      _count++;
      if (_count == 5) {
        yield PageViewState(false, false);
      }
    } else if (event is DataChangeEvent2) {
      String data = event.data ?? "-";
      data += "-$_count";
      _count++;
      yield DataState((data));
    }
  }
}

class MultiState {}

class PageViewState extends MultiState {
  final bool isLoading;
  final bool isNetOk;

  PageViewState(this.isLoading, this.isNetOk);
}

class DataState extends MultiState {
  final String data;

  DataState(this.data);
}

class MultiStateEvent {}

class DataChangeEvent extends MultiStateEvent {
  final String data;

  DataChangeEvent(this.data);
}

class DataChangeEvent2 extends MultiStateEvent {
  final String data;

  DataChangeEvent2(this.data);
}
